package com.hee.erp.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hee.erp.pojo.IPAddress;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Component
public class IPUtils {

    @Resource
    RestTemplate restTemplate;

    public String getCityInfo(String ip) {
        String result = "";
        if("0:0:0:0:0:0:0:1".equals(ip)){
            result = "程序员本地测试";
        }else {
            result = restTemplate.getForObject("https://restapi.amap.com/v5/ip?key=3ebb984f0e51e4619fae932c2d6f23cf&type=4&ip={ip}", String.class, ip);
            IPAddress ipAddress = JSONObject.parseObject(result, IPAddress.class);
            result = String.format("%s-%s-%s-%s-%s",ipAddress.getProvince(),ipAddress.getCity(),ipAddress.getDistrict(),ipAddress.getIsp(),ipAddress.getLocation());
        }
        return result;
    }
}
